/**
 * Created by CZ on 2017/4/6.
 */
module.exports = function ($scope,$rootScope,$interval,$state,RequestService,commonConfig,modelService,$localStorage) {
    $rootScope.DeveloperNavShow = false;  //隐藏nav
    $rootScope.DeveloperHeaderShow = true; //显示头部
    $rootScope.DeveloperFooterShow = true; //显示尾部
    //获取token
    var token = $localStorage.get('cloud').token;
    //检测邮箱是否存在
    $scope.EmailCheckResult = null;
    $scope.EmailCheckExists  = function(email){   // 验证邮箱是否存在
        console.log(email);
        if(email != undefined && email != '' ){
            RequestService.Request(commonConfig.userApi+'user/user/checkEmailExists?email='+email+'&access_token='+token, 'GET').then(function(response){
                console.log(response);
                $scope.EmailCheckResult = response.code;
                switch($scope.EmailCheckResult){
                    case 0: //验证成功
                        $scope.EmailCheckMessage = '邮箱未注册！';
                        break;
                    case 1: //邮箱格式不正确
                        $scope.EmailCheckMessage = '邮箱格式不正确！';
                        break;
                    case 100:  //邮箱在数据库中已存在
                        $scope.EmailCheckMessage = response.message;
                        break;
                }
            },function(error){
                console.log(error);
            });
        }
        //成功：0，邮箱在数据库中已存在：100，邮箱格式不正确：1
    };
    // $scope.img = commonConfig.$webroot+commonConfig.userApi+'user/user/getVerifyImage?access_token='+token;
    //获取图片验证码
    $scope.getVerifyImage = function(){
        RequestService.Request(commonConfig.userApi+'user/verifyimage/getVerifyImage', 'GET').then(function(res){
            // console.log(res);
            $scope.imgKey = res[0];
            $scope.img = res[1];
        },function(error){
            console.log(error);
        });
    };
    $scope.getVerifyImage();
    //验证图片验证码
    $scope.ImageCodeVerifyResult = 0;
    $scope.VerifyImageCode = function(Developer){
        var data = {
            "key": $scope.imgKey,
            "verifyCode": Developer.verifyCode
        };
        RequestService.Request(commonConfig.userApi+'user/verifyimage/verifyCode', 'POST',data).then(function(res){
            // console.log(res);
            $scope.ImageCodeVerifyResult = res.code;
        },function(error){
            console.log(error);
        });
    };

    $scope.PageShow = 0;
    //获取邮箱验证码模块
    $scope.second = 120;
    $scope.getEmailCode = function(Developer){  //获取邮箱验证码
        console.log(Developer);
        $scope.Email = Developer.email;   //绑定名字.
        //验证验证码
        // RequestService.Request(commonConfig.userApi+'user/user/verifyCode?verifyCode='+Developer.verifyCode+'&access_token='+token, 'GET').then(function(response){
        //     console.log(response);
        //     if(response.code == 0){
        //        getEmailCode(Developer);//下一步获取邮箱验证码
        //     }else {  //code = 200
        //         modelService.openTipsModel('验证码错误！');
        //     }
        // },function(error){
        //     console.log(error);
        // });
        RequestService.Request(commonConfig.userApi+'user/verifyemail/getEmailKey?email='+Developer.email, 'POST').then(function(res){
            // console.log(res);
            $scope.emailKey = res;
            $scope.PageShow = 1;
        },function(error){
            console.log(error);
        });
    };
    $scope.submitEmailCode = function(Developer){  //验证邮箱验证码
        console.log(Developer);
        // RequestService.Request(commonConfig.userApi+'user/user/verifyEmail?email='+$scope.Email+'&emailCode='+Developer.EmailCode+'&access_token='+token, 'GET').then(function(response){
        //     console.log(response);
        //     if(response.code == 0){
                $scope.PageShow = 2;  //下一步提交密码
        //     }else {  //code = 200
        //         $scope.EmailCodeCheckResult = '邮箱验证错误！';
        //         //modelService.openTipsModel('邮箱验证错误！');
        //     }
        // },function(error){
        //     console.log(error);
        // });
    };
    $scope.submitPsw = function(Developer){  //提交密码
        console.log(Developer);
        var user = {
            email: $scope.Email,
            newPassword:Developer.password
        };
        RequestService.Request(commonConfig.userApi+'user/user/changePassword?access_token='+token, 'POST',user).then(function(response){
            console.log(response);
            if(response.code == 0){
                //修改成功并跳转至首页
                modelService.openTipsModel('密码修改成功！','DeveloperIndex.Home');
            }else {  //code = 200
                modelService.openTipsModel('密码修改失败:'+response.message);
                $scope.PageShow = 0;  //回到第一步

            }
        },function(error){
            console.log(error);
        })
    };
    // //获取验证码图片
    // $scope.getVerifyCode = function(){
    //     var time = new Date().getTime();
    //     $scope.img = commonConfig.$webroot+commonConfig.userApi+'user/user/getVerifyImage?'+time+'&access_token='+token;
    // };
    // function getEmailCode(Developer){  //获取邮箱验证码
    //     获取邮箱验证码
    //     RequestService.Request(commonConfig.userApi+'user/user/sendEmail?email='+Developer.email+'&access_token='+token, 'GET').then(function(response){
    //         console.log(response);
    //         if(response.code == 100){
    //             modelService.openTipsModel('邮箱未注册！');
    //         }else {
    //             $scope.PageShow = 1;
    //         }
    //     },function(error){
    //         console.log(error);
    //     });
    //
    // }
    //验证邮箱验证码
    $scope.EmailCodeVerifyResult = 0;
    $scope.verifyEmailCode = function(Developer){
        var verifyCode = {
            "key": $scope.emailKey,
            "verifyCode": Developer.EmailCode
        };
        RequestService.Request(commonConfig.userApi+'user/verifyemail/verifyCode', 'POST',verifyCode).then(function(res){
            // console.log(res);
            $scope.EmailCodeVerifyResult = res.code;
        },function(error){
            console.log(error);
        });
    };
};